package com.jinzhi.jinzhidata.service.impl;

import com.jinzhi.jinzhidata.dataobject.Article;
import com.jinzhi.jinzhidata.enums.ArticleStateEnum;
import com.jinzhi.jinzhidata.repository.ArticleRepository;
import com.jinzhi.jinzhidata.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author vansl
 * @description
 * @date 2019-12-28 21:46
 **/
@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    ArticleRepository articleRepository;

    @Override
    public List<Article> selectArticleListPublish(PageRequest pageRequest) {
        Article article = new Article();
        article.setArticleState(ArticleStateEnum.PUBLISH.getCode());
        Example<Article> example = Example.of(article);
        return articleRepository.findAll(example, pageRequest).getContent();
    }

    @Override
    public Long countPublish() {
        Article article = new Article();
        article.setArticleState(ArticleStateEnum.PUBLISH.getCode());
        Example<Article> example = Example.of(article);
        return articleRepository.count(example);
    }

    @Override
    public Article selectByArticleId(String articleId) {
        return articleRepository.findById(articleId).orElse(null);
    }

    @Override
    public List<Article> selectArticleList(PageRequest pageRequest) {
        return articleRepository.findAll(pageRequest).getContent();
    }

    @Override
    public Long count() {
        return articleRepository.count();
    }

    @Override
    public Article deleteArticle(String articleId) {
        Article article = articleRepository.findById(articleId).orElse(null);
        article.setArticleState(ArticleStateEnum.DELETE.getCode());
        return articleRepository.save(article);
    }

    @Override
    public Article saveArticle(Article article) {
        return articleRepository.save(article);
    }

}
